package ui;


import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;

import project_package.Customer;
import project_package.Employee;
import project_package.Product;
import project_package.Sales;
import project_package.SalesLineItem;

import ui.LoginMenu;
import ui.MainUI;
import controller.*;
import project_package.*;



public class StatisticsUI extends JFrame
{

	private JMenuBar jMenuBar;
	private JMenu jMenu;
	private JMenuItem back;
	private JMenuItem signOut;
	private JMenuItem help;
	private Border pEdge;
	private JPanel sales;
	private JPanel rents;
	private JPanel avRents;
	private JPanel avRentsBorder;
	private JPanel unavRents;
	private JPanel scSalesBorder;
	private JPanel total;
	private JLabel salesPrice;
	private JLabel salesNumber;
	private JLabel printAllSales;
	private JLabel rentPrice;
	private JLabel rentNumber;
	private boolean checkRentalEx;
	private JList unavRentID;
	private DefaultListModel unavRentIDM;
	private JList avRentID;
	private DefaultListModel avRentIDM;
	private JList unavRentName;
	private DefaultListModel unavRentNameM;
	private JList avRentName;
	private DefaultListModel avRentNameM;
	private JList unavRentPrice;
	private DefaultListModel unavRentPriceM;
	private JList avRentPrice;
	private DefaultListModel avRentPriceM;
	private JList cusRentDate;
	private DefaultListModel cusRentDateM;
	private JList cusRentDuration;
	private DefaultListModel cusRentDurationM;
	private JList cusRentReturnDate;
	private DefaultListModel cusRentReturnDateM;
	private JScrollPane avRent;
	private JScrollPane scSale;
	private JLabel printAllProducts;
	private JLabel totalMoney;
	private JLabel totalTrans;
	private Employee employee;
	private Customer customer;
	private RentController rentCtrl;
	private Rent rent;
	private DefaultTableModel newRentModel;
	private JTable newRentTable;
	private Sales sale;
	private DefaultTableModel newSaleModel;
	private JTable newSaleTable;
	private SalesController salesCtrl;
	private Sales saleTest;
	private static StatisticsUI instance = null;
	
	public static StatisticsUI getInstance()
	{
		if(instance == null)
		{
			instance = new StatisticsUI();
		}
		return instance;
			
	}
	public StatisticsUI()
	{
		
		
		
		 
		jMenuBar = new JMenuBar();
		
		jMenu = new JMenu("Options");
		jMenuBar.add(jMenu);
		
		back = new JMenuItem("Back");
		event goBack = new event();
		back.addActionListener(goBack);
		jMenu.add(back);
		
		signOut = new JMenuItem("Sign Out");
		event2 leave = new event2();
		signOut.addActionListener(leave);
		jMenu.add(signOut);
		
		
		help = new JMenuItem("Help");
		event3 helper = new event3();
		help.addActionListener(helper);
		jMenu.add(help);
		
		setJMenuBar(jMenuBar);
		
		RentController rentRecCtrl = new RentController();
		ProductController prodCtrl = new ProductController();
		EmployeeController empCtrl = new EmployeeController();
		CustomerController custCtrl = new CustomerController();
		saleTest = new Sales(1, new Customer(1, "test", "test", "test", 1L, 0.0, true), new Employee(1, "gosho", "asd","asd",123L,200.0,true,"asd",0.0,0));
		empCtrl.createEmployee("Admin", "+555 555 555", "Aalborg, Vesterbro 11", 1111112222, 1337.0, true, "password", 0.0, 0);
		HashMap<Integer, Employee> employees = empCtrl.getEmployees();
		custCtrl.createCustomer("Ted", "+555 555 552", "aalborg,  Nibevej 12" , 1111112111, 2.0, true);
		HashMap<Integer, Customer> customers = custCtrl.getCustomers();
		
		rentRecCtrl.createRent("shoes", 150.0, customer, employee);
		
		rentRecCtrl.addRent(rent);
		
		
		String newRentColumns [] = {"#", "Rent", "Customer",  "Employee", "Price", "Lease Date", "Lease Days", "Return Time"};
		newRentModel = new DefaultTableModel(null, newRentColumns);
		
		
		newRentTable = new JTable(newRentModel){
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int colIndex) {
				if (colIndex == 1) return true;
				return false;
			}
		};
		newRentTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
	//	updateList();
		
		
		String newSaleColumns [] = {"#", "Item", "Quantity",  "Price", "Discount", "Total Price"};
		newSaleModel = new DefaultTableModel(null, newSaleColumns);

		newSaleTable = new JTable(newSaleModel){
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int colIndex) {
				if (colIndex == 2) return true;
				return false;
			}
		};
		
		newSaleTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		
		//updateListSale();
		
		StatsController salePrCtrl = new StatsController();
		checkRentalEx=salePrCtrl.checkIfRentsEx();
		salesPrice = new JLabel("Total amount of income from sales : " + salePrCtrl.getTotalSalesPrice() +"kr");
		
		salesNumber = new JLabel("Total amount of sales : " + salePrCtrl.getTotalSalesNumber());
		
	//	printAllSales = new JLabel(salePrCtrl.printAllSales()); //will probably make it a list and use return types of string in the SalesRecords class;
		
		rentPrice = new JLabel("Total amount of income from rentals : " + salePrCtrl.getTotalRentsPrice() + "kr");
		
		rentNumber = new JLabel("Total amount of rentals :" +salePrCtrl.getTotalSalesNumber());
		
	//	printAllRents = new JLabel(salePrCtrl.printAllRents()); //will probably make it a list and use return types of string in the SalesRecords class;
		
		
	//	printAllProducts = newJLabel(salePrCtrl.printAllProducts()); //will probably make it a list and use return types of string in the SalesRecords class;
		
		double totalNr = salePrCtrl.getTotalRentsPrice() + salePrCtrl.getTotalSalesPrice();
		totalMoney = new JLabel("Total Reported Income : " + totalNr + "kr");
		
		double totalCom = salePrCtrl.getTotalRentsNumber() + salePrCtrl.getTotalSalesNumber();
		totalTrans = new JLabel("Total transations : " + totalCom);
		
		pEdge = BorderFactory.createLineBorder(Color.black);
		
		sales = new JPanel();
		sales.setBorder(BorderFactory.createTitledBorder(pEdge, "Sales"));
		sales.setLayout(new BoxLayout(sales, BoxLayout.Y_AXIS));
		sales.add(salesPrice);
		sales.add(salesNumber);
		//sales.add(printAllSales); // to add this when fixed;
		
		rents = new JPanel();
		rents.setBorder(BorderFactory.createTitledBorder(pEdge, "Rentals"));
		rents.setLayout(new BoxLayout(rents, BoxLayout.Y_AXIS));
		rents.add(rentPrice);
		rents.add(rentNumber);
		//rents.add(printAllRents); // to add this when fixed;
		
		 
		
		 unavRentIDM = new DefaultListModel();
		 avRentIDM = new DefaultListModel();
		 unavRentNameM = new DefaultListModel();
		 avRentNameM = new DefaultListModel();
		 unavRentPriceM = new DefaultListModel();
		 avRentPriceM = new DefaultListModel();
		 cusRentDateM = new DefaultListModel();
		 cusRentDurationM = new DefaultListModel();
		 cusRentReturnDateM = new DefaultListModel();
		 
	//	if(checkRentalEx != false)
	//	{
			 unavRentIDM.addElement(salePrCtrl.printUnavableRentID());
			 avRentIDM.addElement(salePrCtrl.printAvableRentID());
			 unavRentNameM.addElement(salePrCtrl.printUnavableRentName());
			 avRentNameM.addElement(salePrCtrl.printAvableRentName());
			 unavRentPriceM.addElement(salePrCtrl.printUnavableRentPrice());
			 avRentPriceM.addElement(salePrCtrl.printAvableRentPrice());
			 cusRentDateM.addElement(salePrCtrl.printCustomerDateRent());
			 cusRentDurationM.addElement(salePrCtrl.printCustomerDurationRent());
			 cusRentReturnDateM.addElement(salePrCtrl.printCustomerSetReturn());
			 
			 unavRentID = new JList(unavRentIDM);
			 unavRentID.setLayoutOrientation(JList.VERTICAL_WRAP);
			 avRentID = new JList(avRentIDM);
			 avRentID.setLayoutOrientation(JList.VERTICAL_WRAP);
			 unavRentName = new JList(unavRentNameM);
			 unavRentName.setLayoutOrientation(JList.VERTICAL_WRAP);
			 avRentName = new JList(avRentNameM);
			 avRentName.setLayoutOrientation(JList.VERTICAL_WRAP);
			 unavRentPrice = new JList(unavRentPriceM);
			 unavRentPrice.setLayoutOrientation(JList.VERTICAL_WRAP);
			 avRentPrice = new JList(avRentPriceM);
			 avRentPrice.setLayoutOrientation(JList.VERTICAL_WRAP);
			 cusRentDate = new JList(cusRentDateM);
			 cusRentDate.setLayoutOrientation(JList.VERTICAL_WRAP);
			 cusRentDuration = new JList(cusRentDurationM);
			 cusRentDuration.setLayoutOrientation(JList.VERTICAL_WRAP);
			 cusRentReturnDate = new JList(cusRentReturnDateM);
			 cusRentReturnDate.setLayoutOrientation(JList.VERTICAL_WRAP);
		//}
		
		avRents = new JPanel();
		avRents.setLayout(new BoxLayout(avRents,BoxLayout.X_AXIS));
		avRents.add(avRentID);
		avRents.add(avRentName);
		avRents.add(avRentPrice);
		
		unavRents = new JPanel();
		unavRents.setLayout(new BoxLayout(unavRents,BoxLayout.X_AXIS));
		unavRents.add(unavRentID);
		unavRents.add(unavRentName);
		unavRents.add(unavRentPrice);
		unavRents.add(cusRentDate);
		unavRents.add(cusRentDuration);
		unavRents.add(cusRentReturnDate);
		
		avRent = new JScrollPane(newRentTable);
		avRent.setPreferredSize(new Dimension(700, 150));
	    scSale = new JScrollPane(newSaleTable);
	    scSale.setPreferredSize(new Dimension(700,150));
		
	
		
		total = new JPanel();
		total.setBorder(BorderFactory.createTitledBorder(pEdge, "General Information"));
		total.setLayout(new BoxLayout(total, BoxLayout.Y_AXIS));
		total.add(totalMoney);
		total.add(totalTrans);
		//total.add(printAllProducts; // to add when fixed;
		
		avRentsBorder = new JPanel();
		avRentsBorder.setBorder(BorderFactory.createTitledBorder(pEdge, "Rents History"));
		avRentsBorder.setLayout(new BoxLayout(avRentsBorder, BoxLayout.Y_AXIS));
		avRentsBorder.add(avRent);
		
		scSalesBorder = new JPanel();
		scSalesBorder.setBorder(BorderFactory.createTitledBorder(pEdge, "Sales History"));
		scSalesBorder.setLayout(new BoxLayout(scSalesBorder, BoxLayout.Y_AXIS));
		scSalesBorder.add(scSale);
		
		getContentPane().add(sales, BorderLayout.CENTER);
		getContentPane().add(rents, BorderLayout.WEST);
		getContentPane().add(total, BorderLayout.EAST);
		getContentPane().add(avRentsBorder, BorderLayout.SOUTH);
		getContentPane().add(scSalesBorder, BorderLayout.NORTH);
		
		setVisible(true);
	

	}
	
		public class event implements ActionListener
		{
			public void actionPerformed(ActionEvent goBack)
			{
				setVisible(false);
				MainUI gui = new MainUI();
				gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				gui.pack();
				gui.setLocationRelativeTo(null);
				gui.setTitle("Main");
				gui.setResizable(false);
				
			}
		}
		public class event2 implements ActionListener
		{
			public void actionPerformed(ActionEvent leave)
			{
				
			}
		}
		public class event3 implements ActionListener
		{
			public void actionPerformed(ActionEvent help)
			{
				
			}
		}
		
		public static void main(String args[])
		{
			try {
		           
		        UIManager.setLookAndFeel(
		            UIManager.getSystemLookAndFeelClassName());
		    } 
		    catch (UnsupportedLookAndFeelException e) {
		       // handle exception
		    }
		    catch (ClassNotFoundException e) {
		       // handle exception
		    }
		    catch (InstantiationException e) {
		       // handle exception
		    }
		    catch (IllegalAccessException e) {
		       // handle exception
		    }
			
			StatisticsUI gui = new StatisticsUI();
			gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			gui.pack();
			gui.setLocationRelativeTo(null);
			gui.setTitle("Statistics");
			gui.setResizable(false);
		}
		private void clearList()
		{
			DefaultTableModel dm = (DefaultTableModel)newRentTable.getModel();
			while(dm.getRowCount() > 0)
			    dm.removeRow(0);
		}
		private void updateList() 
		{
			clearList();
			String[][] list = rentCtrl.getHistory();
			System.out.println(list.length);
			for(int i = 0; i < list.length; i++)
	        {
	            if (list[i] != null)
	            { 
	            	newRentModel.addRow(new Object[]{ list [i] [0], list [i] [2], list [i] [3], list [i] [4], list [i] [5], list [i] [6], list [i] [7]});
	            	
	            }
	        }
		}
		private void clearListSale()
		{
			DefaultTableModel dm = (DefaultTableModel)newSaleTable.getModel();
			while(dm.getRowCount() > 0)
			    dm.removeRow(0);
		}
		
		private void updateListSale() 
		{
			clearListSale();
			ArrayList<SalesLineItem> list = salesCtrl.getProductList(sale);
			for(SalesLineItem item : list)
			{
				Product product = item.getProduct();
	            int productPrice = (int) (item.getQuantity() * (product.getPrice()*1-product.getDiscount()));
				newSaleModel.addRow(new Object[]{ product.getID(), product.getName(), item.getQuantity(), product.getPrice(), product.getDiscount(), productPrice});	
			}
		}


}
